<?php
include("utils.php");
if($_SESSION['siteuserId']=="")
{
	header("location:start.php");
}
// if($_POST['mode']=="change_status")
// {
// 	change_status();
// }
// else
// {
	main_content("main();");
//}

// function change_status()
// {
//   echo $sql = "UPDATE ".SITEMASTER." SET status = if(status = 'N','Y','N') WHERE adsiteId = '".$_POST['Id']."' ";
// 	mysql_query($sql);
// 	$GLOBALS['err_msg']="Status Changed Successfully";
// 	main_content("main();");
// 
// }

function main()
{

if($_SESSION['languageId']!="")
{
	$languageId=$_SESSION['languageId'];
}
else
{
	$languageId='1';
}

if($_REQUEST['mode']=="campaign_statistics")
{
//echo "<script>alert('hi')</script>";
if($_REQUEST['campaign_status']!='' && $_REQUEST['ad_type']=='')
{
	if($_REQUEST['campaign_status']=='running')
	{
	//echo "<script>alert('123456')</script>";
		$sql_details="select * from adcampaign where siteuserId = '".$_SESSION['siteuserId']."' and start_date<now() and end_date>now()";
	}
	else if($_REQUEST['campaign_status']=='all')
	{
		$sql_details="select * from adcampaign where siteuserId = '".$_SESSION['siteuserId']."'";
	}
	else
	{
		$sql_details="select * from adcampaign where siteuserId = '".$_SESSION['siteuserId']."'";
	}
}


else if($_REQUEST['campaign_status']=='' && $_REQUEST['ad_type']!='')
{
	$sql_details="SELECT * FROM `adcampaign` WHERE `campaign_id` in (select campaign_id from `adgroup` where adgroup_network in (select network_id from `create_network` where ad_type = '".$_REQUEST['ad_type']."')) and siteuserId = '".$_SESSION['siteuserId']."'";
}


else if($_REQUEST['campaign_status']!='' && $_REQUEST['ad_type']!='')
{
	if($_REQUEST['campaign_status']=='running')
	{
		$sql_details="SELECT * FROM `adcampaign` WHERE `campaign_id` in (select campaign_id from `adgroup` where adgroup_network in (select network_id from `create_network` where ad_type = '".$_REQUEST['ad_type']."')) and siteuserId = '".$_SESSION['siteuserId']."' and start_date<now() and end_date>now()";
		
	}
	else if($_REQUEST['campaign_status']=='all')
	{
		$sql_details="SELECT * FROM `adcampaign` WHERE `campaign_id` in (select campaign_id from `adgroup` where adgroup_network in (select network_id from `create_network` where ad_type = '".$_REQUEST['ad_type']."')) and siteuserId = '".$_SESSION['siteuserId']."'";
	}
	else
	{
		$sql_details="SELECT * FROM `adcampaign` WHERE `campaign_id` in (select campaign_id from `adgroup` where adgroup_network in (select network_id from `create_network` where ad_type = '".$_REQUEST['ad_type']."')) and siteuserId = '".$_SESSION['siteuserId']."'";
	}
}
}

else
{

 $sql_details="select * from adcampaign where siteuserId = '".$_SESSION['siteuserId']."'";

	//echo $sql_details;
}
//echo $sql_details;SELECT * FROM `adcampaign` WHERE `campaign_id` in (select campaign_id from `adgroup` where adgroup_network in (select network_id from `create_network` where ad_type = 'text'))
$query_details=mysql_query($sql_details);
$num=mysql_num_rows($query_details);

//  $query_site_name=mysql_query("select adSiteName from adsite_master where siteuserId='".$_SESSION['siteuserId']."' and langId='".$languageId."'");

?>

<script language="JavaScript">
function search_campaign_statistics()
{
//alert('hi');
document.ad_campaign_statistics.mode.value="campaign_statistics";
document.ad_campaign_statistics.submit();
}

function under_display_function(id)
{
var id = id;
//alert(id);
	if(document.getElementById('under_table_'+id).style.display == 'none')
	{
	document.getElementById('under_table_'+id).style.display = '';
	}
	
	else
	{
	document.getElementById('under_table_'+id).style.display = 'none';
	}

}

function under_ad_display_function(id)
{

	if(document.getElementById('under_ad_table_'+id).style.display == 'none')
	{
	document.getElementById('under_ad_table_'+id).style.display = '';
	}
	
	else
	{
	document.getElementById('under_ad_table_'+id).style.display = 'none';
	}

}
</script>
<!--<SCRIPT language=javascript src="calender/js/popcalendar.js"></SCRIPT>-->
<script language="javascript" type="text/javascript" src="JavaScripts/datetimepicker.js"></script>

<form name="ad_campaign_statistics" action="" method="POST" id="ad_campaign_statistics" onsubmit="return search_campaign_statistics();">
<input type="hidden" name="mode" value="">
<br />
<br />

<table width="90%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td valign="top"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
                  <tr>
                    <td>&nbsp;</td>
                    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderTopLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxTopBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderTopRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                      <tr>
                        <td class="PinkTwosideBorder"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
                            <tr>
                              <td class="BoxContent"><table id="spacesList" style="width:100%;margin-left:1px;position:relative" align="center" border="0" >

<tr>
    <td colspan="2" align="center" class="sky font-18" style="padding-bottom:20px;">
	Campaign Statistics
	</td>
<?php
if($GLOBALS['err_msg']!="")
{
?>
<tr>
    <td colspan="2" align="center" class="font-11 latest_heading2" style="padding-bottom:10px;">
	<strong><?php echo $GLOBALS['err_msg'];?></strong>
	</td>
</tr>
<?php
}
?>
<?php

$select_site="select * from ".SITEMASTER." order by adsiteId";
$exe_site=mysql_query($select_site);
?>
  <tr>
	<td style="text-align:left;white-space:nowrap;padding-bottom:10px;" colspan="" valign="middle">


      <span class="font-12 black advertise_tableTxt">

      Status:&nbsp;&nbsp;&nbsp;
		<select name="campaign_status" id="campaign_status">
		<option value="">Running/Paused</option>
		<option value="running">Running</option>
		<option value="all">All</option>
		</select>
      </span>

<span class="font-12 black advertise_tableTxt">
&nbsp;&nbsp;&nbsp;
        Ad Type:&nbsp;&nbsp;
        <select id="ad_type" name="ad_type">
          <option value="">Select</option>
	  <option value="text">Text</option>
	  <option value="banner">Banner</option>
	  <option value="thumbnail">Thumbnail</option>
	  <option value="commercial_break">Commercial Break</option>
	  <option value="java_script">JavaScript</option>
	  <option value="custom">Custom</option>
          </select>
</span>

	<span class="font-12 black advertise_tableTxt">
	&nbsp;&nbsp;
	<input type="submit" name="campaign_statistics" id="campaign_statistics" value="Go" class="blank-button-big-add" />
	</span>

    </td>

  </tr>


  <tr>

    <td colspan="2">

<table border="1" cellspacing="1" cellpadding="0" width="100%">
 <TR>
  <TD align="center">
   <table width="100%" class="login_txt" cellspacing="1" cellpadding="0" border="0">
    <tr style="background-color:gray">
        <th class="listHeader" align="center" nowrap="true">
        <strong>Campaign / Ad Group</strong>
        </th>
	<th class="listHeader" align="center" nowrap="true">
        <strong>Contents</strong>
        </th>
	<th class="listHeader" align="center" nowrap="true">
         Daily Budget
        </th>
	<th class="listHeader" align="center" nowrap="true">
         Daily Budget Spent
        </th>
	<th class="listHeader" align="center" nowrap="true">
         Total Budget
       </th>
	<th class="listHeader" align="center" nowrap="true">
         Total Budget Spent
       </th>
	<th class="listHeader" align="center" nowrap="true">
         Toatl Imps
       </th>
	<th class="listHeader" align="center" nowrap="true">
         Total Clicks
       </th>
	<th class="listHeader" align="center" nowrap="true">
         Avg. CPC
       </th>
	<th class="listHeader" align="center" nowrap="true">
         CTR%
       </th>

	    <?php
		if($num>0)
		{

		$loop=0;
		while($rec_details=mysql_fetch_array($query_details))
		{
		$loop++;
		if($loop%2 == 0) 
		{
		$class1="normalActive2";
		$class2="normal2";
		$class3="normal2";
		$class4="gray_sitename2";
		}
		else 
		{
		$class1="normalActive";
		$class2="normal";
		$class3="normal";
		$class4="white_sitename2";
		}
$query_adgroup = " select * from adgroup where campaign_id = '".$rec_details['campaign_id']."' and siteuserId = '".$_SESSION[siteuserId]."' ";
$res_adgroup = mysql_query($query_adgroup);
$total_adgroup = mysql_num_rows($res_adgroup);

$query_ad = " select * from adgroup_ad where campaign_id = '".$rec_details['campaign_id']."'";
$res_ad = mysql_query($query_ad);
$total_ad = mysql_num_rows($res_ad);
	    ?>

        <tr onmouseover="this.className='<?php echo $class1;?>'" onmouseout="this.className='<?php echo $class2;?>'" class="<?php echo $class3;?>">

        <td class="<?php echo $class4;?>" align="left" style="cursor:pointer;" id="plus_id_<?php $rec_details['campaign_id'];?>" onclick="return under_display_function(<?php echo $rec_details['campaign_id']; ?>);">++&nbsp;&nbsp;<?php echo $rec_details['camp_name']; ?>
	</td>

	<td class="<?php echo $class4;?>" align="left"><?php echo $total_adgroup;?> Grops, <?php echo $total_ad;?> Ads 
                        </td>

	<td class="<?php echo $class4;?>" align="left"><?php echo $rec_details['daily_budget'];?>
                        </td>

		<td class="<?php echo $class4;?>" align="left">0.00
                        </td>


<td class="<?php echo $class4;?>" align="left"><?php echo $rec_details['total_budget'];?></td>

		<td class="<?php echo $class4;?>" align="left">0.00
                        </td>

		<td class="<?php echo $class4;?>" align="left">N/A
                        </td>
		
		<td class="<?php echo $class4;?>" align="left">N/A</td>
		
		<td class="<?php echo $class4;?>" align="left">N/A
                        </td>
		<td class="<?php echo $class4;?>" align="left">N/A
                        </td>
        </tr>

<!-- 	############## start ad group ########### -->
<tr id="under_table_<?php echo $rec_details['campaign_id']; ?>" style="display:none;">
		<td>
			<table bgcolor="#e8e8e8" cellpadding="0" cellspacing="0">
				<tr><TD height="8px"></TD></tr>
				<tr style="background-color:gray">
					<th class="listHeader" align="left" nowrap="true">&nbsp;</th>
					<th class="listHeader" align="left" nowrap="true">Ad Group Name</th>
					<th class="listHeader" align="left" nowrap="true">Ad Group Type</th>
					<th class="listHeader" align="left" nowrap="true">Daily Budget</th>
					<th class="listHeader" align="left" nowrap="true">Nework Type</th>
					<th class="listHeader" align="left" nowrap="true">Bid</th>
				</tr>	
<?php while($row_adgroup = mysql_fetch_array($res_adgroup)){

$select_adgroup_bid="select ad_type, bid from create_network where network_id = '".$row_adgroup['adgroup_network']."'";
$exe_adgroup_bid=mysql_query($select_adgroup_bid);
$row_adgroup_bid=mysql_fetch_array($exe_adgroup_bid);

$adgroup_type=$row_adgroup_bid['ad_type'];
$adgroup_bid_price=$row_adgroup_bid['bid'];
	
$query_adgroup_ad = "select * from adgroup_ad where adgroup_id = '".$row_adgroup['adgroup_id']."' and campaign_id = '".$rec_details['campaign_id']."' ";
$res_adgroup_ad   = mysql_query($query_adgroup_ad);
$total_adgroup_ad = mysql_num_rows($res_adgroup_ad);

?>
				<tr>
					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer">&nbsp;</td>
					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer;" id="plus_ad_id_<?php echo $row_adgroup['adgroup_id']; ?>" onclick="return under_ad_display_function(<?php echo $row_adgroup['adgroup_id']; ?>);">+&nbsp;&nbsp;<?php echo $row_adgroup['adgroup_name']; ?>
					</td>

					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer"><?php echo $adgroup_type; ?></td>
					
					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer"><?php echo $row_adgroup['adgroup_daily_budget']; ?></td>

					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer"><?php echo $row_adgroup['network_type']; ?></td>

					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer"><?php echo $adgroup_bid_price; ?></td>
	
				</tr>	


				<tr id="under_ad_table_<?php echo $row_adgroup['adgroup_id']; ?>" style="display:none;" ><TD colspan="8"><table> <!--Start Displaying Ad under adgrop-->

				<tr style="background-color:gray">&nbsp;&nbsp;&nbsp;&nbsp;
					<th class="listHeader" align="center" nowrap="true">&nbsp;</th>
					<th class="listHeader" align="center" nowrap="true">Ad Name</th>
					<th class="listHeader" align="center" nowrap="true">Ad Type</th>
					<th class="listHeader" align="center" nowrap="true">Description</th>
					<th class="listHeader" align="center" nowrap="true">Total Imps</th>
					<th class="listHeader" align="center" nowrap="true">Total Clicks</th>
					<th class="listHeader" align="center" nowrap="true">Avg. CPC</th>
					<th class="listHeader" align="center" nowrap="true">CTR %</th>
				</tr>
				
				<?php if($total_adgroup_ad < 1) { ?>
				<tr>
					
					<TD class="<?php echo $class4;?>" colspan="9" align="center"><?php echo NO_DATA; ?></TD>
				</tr>
				
				<?php } while($row_adgroup_ad = mysql_fetch_array($res_adgroup_ad)){ ?>

				<tr>&nbsp;&nbsp;&nbsp;&nbsp;
					
					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer">&nbsp;</td>
					
					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer"><?php echo $row_adgroup_ad['heading']; ?></td>

					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer"><?php echo $adgroup_type; ?></td>

					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer"><?php echo $row_adgroup_ad['ad_description']; ?></td>

					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer">00</td>

					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer">00</td>

					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer">0.00</td>

					<td class="<?php echo $class4;?>" align="left" style="cursor:pointer">0.00%</td>
	
				</tr>

				

											 <?php   }   ?>

				</table></TD></tr> <!--End Displaying Ad under adgrop-->
<?php } ?> 
				
			</table>
		</TD>
	</tr>


<!-- ########## end ad group ############ -->
	<?php
	}
	} // end if
	else
	{
	?>
	   <tr><TD colspan="10" align="center" class="font-12 black advertise_tableTxt">No records found</TD></tr>

	<?php
	}
	?>

       </table>

    </td>
  </tr>
</table>

   </td>
  </tr>
</table></td>
                            </tr>
                        </table></td>
                      </tr>
                      <tr>
                        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="11"><img src="images/PinkBorderBottomLeft.jpg" alt="" width="11" height="11"></td>
                              <td class="PinkBoxBottomBg"><img src="images/spacer.gif" alt="" width="10" height="11"></td>
                              <td width="11"><img src="images/PinkBorderBottomRight.jpg" alt="" width="11" height="11"></td>
                            </tr>
                        </table></td>
                      </tr>
                    </table></td>
                    <td>&nbsp;</td>
                    </tr>
                </table></td>
              </tr>
            </table>
</form>
            
<?php
}
?>